Function Show-Menu
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $True)]
        [System.Xml.XmlElement]
        $private:XmlMenuRoot,
        
        [Parameter(Mandatory = $True)]
        [Microsoft.PowerShell.Host.ISE.ISEMenuItem]
        $private:Menu
    )
    
    Process
    {
        ForEach ($private:XmlMenu in $XmlMenuRoot.GetEnumerator())
        {
            # If the attribute "DisplayName" is set, use its' value, otherwise use the name of the element
            If (-Not [String]::IsNullOrEmpty($XmlMenu.DisplayName))
            {
                $private:Name = $XmlMenu.DisplayName
            }
            Else
            {
                $private:Name = $XmlMenu.Name
            }
            
            # Remove any menu items with the same name
            $private:MenuItemsToRemove = $Menu.SubMenus.GetEnumerator() | Where-Object { $_.DisplayName -Eq $Name }
            ForEach ($private:MenuItemToRemove in $MenuItemsToRemove)
            {
                If ($MenuItemToRemove -Ne $Null)
                {
                    $Menu.SubMenus.Remove($MenuItemToRemove) | Out-Null
                }
            }
            
            # We currently don't have a way to specify shortcuts.
            $private:Shortcut = $Null
            
            # Retrieve the property from the root node
            # Needed so that text nodes will be returned as a string containing the text value
            # as opposed to an XmlElement (which is what you get by iterating through the enumerator)
            #$XmlMenuItem = $XmlMenuRoot.($XmlMenu.Name)
            #If ($XmlMenuItem -Is [System.Xml.XmlElement])
            If ($XmlMenu."#text" -Eq $Null)
            {
                # This is an element, indicating that there are child nodes
                $private:SubMenu = $Menu.SubMenus.Add($Name, $Null, $Shortcut)
                Show-Menu $XmlMenu $SubMenu
            }
            #ElseIf ($XmlMenuItem -Is [String])
            Else
            {
                $private:ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock($XmlMenu."#text")
                $Menu.SubMenus.Add($Name, $ScriptBlock, $Shortcut) | Out-Null
            }
        }
    }
}

Export-ModuleMember Show-Menu
